import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index.js'
 
Vue.use(VueRouter)

const routes = [{
		path: '/',
		name: '/',
		redirect: '/shop'
	},
	{
		path: '/tiroGuide',
		name: 'tiroGuide',
		component: () => import('../component01/tiroGuide.vue')
	},
	{
		path: '/abc',
		name: 'abc',
		component: () => import('../component01/componentsChild/abc')
	},
	{
		path: '/about',
		name: 'about',
		component: () => import('../component01/about.vue')
	},
	{
		path: '/contact',
		name: 'contact',
		component: () => import('../component01/contact.vue')
	},
	{
		path: '/login',
		name: 'log',
		component: () => import('../component01/log.vue')
	},
	{
		path: '/termsOfUse',
		name: 'termsOfUse',
		component: () => import('../component01/termsOfUse.vue')
	},
	{
		path: '/privacyPolicy',
		name: 'termsOprivacyPolicyfUse',
		component: () => import('../component01/privacyPolicy.vue')
	},
	{
		path: '/helpPage',
		name: 'helpPage',
		component: () => import('../component01/helpPage.vue')
	},
	{
		path: '/signUp',
		name: 'signUp',
		component: () => import('../component01/signUp.vue')
	},
	{
		path: '/fag',
		name: 'fag',
		component: () => import('../component01/componentsChild/fag')
	},
	{
		path: '/help',
		name: 'help',
		component: () => import('../component01/componentsChild/help')
	},
	{
		path: '/home',
		name: '/home',
		component: () => import('../views/Home/Home.vue'),
	},
	{
		path: '/shop',
		name: 'Shop',
		component: () => import('../views/Shop/shop')
	},
	{
		path: '/shopdetail',
		name: 'ShopDetail',
		component: () => import('../views/ShopDetail/shopdetail'),
	},
	{
		path: '/account',
		name: 'account',
		component: () => import('../component01/account'),
		meta: {
			requiresAuth: true // 要求验证的页面,在此情况下其子页面也会被验证.
		},
	},
	{
		path: '/accountMenu',
		name: 'accountMenu',
		component: () => import('../component01/accountMenu'),
	},
	{
		path: '/user',
		name: '/user',
		component: () => import('../component01/User'),
		meta: {
			requiresAuth: true // 要求验证的页面,在此情况下其子页面也会被验证.
		},
		children: [{
				path: '/profile',
				name: 'profile',
				component: () => import('../component01/componentsChild/profile'),
			}, {
				path: '/application',
				name: 'application',
				component: () => import('../component01/componentsChild/application')
			}, {
				path: '/balance',
				name: 'balance',
				component: () => import('../component01/componentsChild/balance')
			}, {
				path: '/accountSettings',
				name: 'accountSettings',
				component: () => import('../component01/componentsChild/accountSettings')
			}, {
				path: '/referral',
				name: 'referral',
				component: () => import('../component01/componentsChild/referral')
			},
			{
				path: '/rewardPoints',
				name: 'rewardPoints',
				component: () => import('../component01/componentsChild/rewardPoints'),
			}
		]
	}
]

const router = new VueRouter({
	routes
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
	if (document.querySelector(".main-box")) {
		document.querySelector(".main-box").scrollTop = 0
	}
	if (to.matched.some(record => record.meta.requiresAuth)) { // 哪些需要验证
		// 判断是否登录
		console.log(to)
		let token = window.localStorage.getItem('token')
		if (token) {
			if (store.state.userinfo.status == 3 && to.fullPath != "/account") {
				// 没有绑定过邮箱 
				Vue.prototype.$bus.emit('showConfirmEmail')
			} else {
				next()
			}
		} else {
			next({
				path: '/login', // 验证失败要跳转的页面
				query: {
					redirect: to.fullPath // 要传的参数
				}
			})
		}
	} else {
		next() // 确保一定要调用 next()
	}
})

export default router
